package org.shilong.UI.businessCard;

import org.shilong.MContext.Businesscard.BusinessCardContext;
import org.shilong.UI.R;
import org.shilong.Utility.ProgressDialogUtility;
import org.shilong.Utility.ToastUtility;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 修改名片页
 * @author cesc
 *
 */
public class AlertBusinessCardFrame extends Activity implements OnClickListener {

	private TextView bannerTextivew;

	private EditText businesscard_input_telephonenum, businesscard_input_com_name, businesscard_input_buss, businesscard_input_domain_content, businesscard_input_sex, businesscard_input_age;

	private  Button business_button_alert;

	private Bundle bundle;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView( R.layout.layout_alert_business_card_content);

		bundle = getIntent().getExtras();

		initViews();

		registerEvents();
	}

	private void initViews(){

		bannerTextivew = ( TextView )this.findViewById( R.id.business_card_banner_textivew );
		bannerTextivew.setText( "修改名片" );

		businesscard_input_telephonenum = ( EditText )this.findViewById( R.id.businesscard_input_telephonenum );
		businesscard_input_com_name = ( EditText )this.findViewById( R.id.businesscard_input_com_name );
		businesscard_input_buss = ( EditText )this.findViewById( R.id.businesscard_input_buss );
		businesscard_input_domain_content = ( EditText )this.findViewById( R.id.businesscard_input_domain_content );
		businesscard_input_sex = ( EditText )this.findViewById( R.id.businesscard_input_sex );
		businesscard_input_age = ( EditText )this.findViewById( R.id.businesscard_input_age );

		businesscard_input_telephonenum.setText( bundle.getString( "telephonenum" ));
		businesscard_input_com_name.setText( bundle.getString( "com_name" ));
		businesscard_input_buss.setText( bundle.getString( "buss" ));
		businesscard_input_domain_content.setText( bundle.getString( "domain" ));
		businesscard_input_sex.setText(bundle.getString( "sex" ));
		businesscard_input_age.setText(bundle.getString( "age" ));

		business_button_alert = ( Button )this.findViewById( R.id.business_button_alert );
	}


	/**
	 * 初始化事件
	 */
	private void registerEvents(){

		business_button_alert.setOnClickListener( this );

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch ( arg0.getId() ) {
		case R.id.business_button_alert:

			alertBusinessCardLogic();

			break;

		default:
			break;
		}

	}


	private static String telephone  = null;
	private static String company_name  = null;
	private static String buesiness  = null;
	private static String domain  = null;
	private static String age  = null;
	private static String sex  = null;

	private void alertBusinessCardLogic(){

		telephone  = null;
		company_name  = null;
		buesiness  = null;
		domain  = null;
		age  = null;
		sex  = null;

		System.out.println( "修改名片...." );

		telephone = businesscard_input_telephonenum.getText().toString();
		company_name = businesscard_input_com_name.getText().toString();
		buesiness = businesscard_input_buss.getText().toString();
		domain = businesscard_input_domain_content.getText().toString();
		age = businesscard_input_age.getText().toString();
		sex = businesscard_input_sex.getText().toString();


		if( TextUtils.isEmpty( telephone )){

			ToastUtility.showToast(getApplicationContext(), "输入电话号码" );
			return ;
		}


		if ( TextUtils.isEmpty( company_name )) {
			ToastUtility.showToast(getApplicationContext(), "输入公司名" );
			return ;
		}

		if ( TextUtils.isEmpty( buesiness )) {
			ToastUtility.showToast(getApplicationContext(), "输入行业" );
			return ;
		}

		if ( TextUtils.isEmpty( domain )) {
			ToastUtility.showToast(getApplicationContext(), "输入领域" );
			return ;
		}

		if ( TextUtils.isEmpty( age )) {
			ToastUtility.showToast(getApplicationContext(), "输入年龄" );
			return ;
		}

		if ( TextUtils.isEmpty( sex )) {
			ToastUtility.showToast(getApplicationContext(), "输入性别" );
			return ;
		}

		UpdateTask task = new UpdateTask( AlertBusinessCardFrame.this );

		task.execute( "123", telephone, company_name, buesiness, domain, sex, age, "alert" );
	}

	/**
	 * 更新名片对话框
	 * @author cesc
	 *
	 */
	private class UpdateTask extends AsyncTask<String, String, String>{

		private Context con;

		public UpdateTask( Context mContext ){
			this.con = mContext;
		}

		@Override
		protected String doInBackground(String... params) {

			String rlt = "ohshit";

			rlt = (String)BusinessCardContext.createInstance( BusinessCardContext.TYPE_ALERT_BUSINESS_CARD ).getRlt(params);

			return rlt;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub

			ProgressDialogUtility.cancelDialog();

			System.out.println( "result:" + result );

			if( result.equals("update_bussiness_card_successed")){

				ToastUtility.showToast(con, "更新成功");

			}else if( result.equals( "update_bussiness_card_failed" )){

				ToastUtility.showToast(con, "更新失败");

			}

		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub

			ProgressDialogUtility.showDialog(con, "更新中..");
		}

	}
}
